
(function(window, document, jQuery) {

	var isInputSupported = 'placeholder' in document.createElement('input'),
	    isTextareaSupported = 'placeholder' in document.createElement('textarea'),
	    prototype = jQuery.fn,
	    valHooks = jQuery.valHooks,
	    hooks,
	    placeholder;

	if (isInputSupported && isTextareaSupported) {

		placeholder = prototype.placeholder = function() {
			return this;
		};

		placeholder.input = placeholder.textarea = true;

	} else {

		placeholder = prototype.placeholder = function() {
			var jQuerythis = this;
			jQuerythis
				.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
				.not('.placeholder')
				.bind({
					'focus.placeholder': clearPlaceholder,
					'blur.placeholder': setPlaceholder
				})
				.data('placeholder-enabled', true)
				.trigger('blur.placeholder');
			return jQuerythis;
		};

		placeholder.input = isInputSupported;
		placeholder.textarea = isTextareaSupported;

		hooks = {
			'get': function(element) {
				var jQueryelement = jQuery(element);
				return jQueryelement.data('placeholder-enabled') && jQueryelement.hasClass('placeholder') ? '' : element.value;
			},
			'set': function(element, value) {
				var jQueryelement = jQuery(element);
				if (!jQueryelement.data('placeholder-enabled')) {
					return element.value = value;
				}
				if (value == '') {
					element.value = value;
					// Issue #56: Setting the placeholder causes problems if the element continues to have focus.
					if (element != document.activeElement) {
						// We can't use `triggerHandler` here because of dummy text/password inputs :(
						setPlaceholder.call(element);
					}
				} else if (jQueryelement.hasClass('placeholder')) {
					clearPlaceholder.call(element, true, value) || (element.value = value);
				} else {
					element.value = value;
				}
				// `set` can not return `undefined`; see http://jQuerysapi.info/jQueryquery/1.7.1/val#L2363
				return jQueryelement;
			}
		};

		isInputSupported || (valHooks.input = hooks);
		isTextareaSupported || (valHooks.textarea = hooks);

		jQuery(function() {
			// Look for forms
			jQuery(document).delegate('form', 'submit.placeholder', function() {
				// Clear the placeholder values so they don't get submitted
				var jQueryinputs = jQuery('.placeholder', this).each(clearPlaceholder);
				setTimeout(function() {
					jQueryinputs.each(setPlaceholder);
				}, 10);
			});
		});

		// Clear placeholder values upon page reload
		jQuery(window).bind('beforeunload.placeholder', function() {
			jQuery('.placeholder').each(function() {
				this.value = '';
			});
		});

	}

	function args(elem) {
		// Return an objQueryect of element attributes
		var newAttrs = {},
		    rinlinejQuery = /^$\d+j$/;
		jQuery.each(elem.attributes, function(i, attr) {
			if (attr.specified && !rinlinejQuery.test(attr.name)) {
				newAttrs[attr.name] = attr.value;
			}
		});
		return newAttrs;
	}

	function clearPlaceholder(event, value) {
		var input = this,
		    jQueryinput = jQuery(input);
		if (input.value == jQueryinput.attr('placeholder') && jQueryinput.hasClass('placeholder')) {
			if (jQueryinput.data('placeholder-password')) {
				jQueryinput = jQueryinput.hide().next().show().attr('id', jQueryinput.removeAttr('id').data('placeholder-id'));
				// If `clearPlaceholder` was called from `jQuery.valHooks.input.set`
				if (event === true) {
					return jQueryinput[0].value = value;
				}
				jQueryinput.focus();
			} else {
				input.value = '';
				jQueryinput.removeClass('placeholder');
				input == document.activeElement && input.select();
			}
		}
	}

	function setPlaceholder() {
		var jQueryreplacement,
		    input = this,
		    jQueryinput = jQuery(input),
		    jQueryorigInput = jQueryinput,
		    id = this.id;
		if (input.value == '') {
			if (input.type == 'password') {
				if (!jQueryinput.data('placeholder-textinput')) {
					try {
						jQueryreplacement = jQueryinput.clone().attr({ 'type': 'text' });
					} catch(e) {
						jQueryreplacement = jQuery('<input>').attr(jQuery.extend(args(this), { 'type': 'text' }));
					}
					jQueryreplacement
						.removeAttr('name')
						.data({
							'placeholder-password': true,
							'placeholder-id': id
						})
						.bind('focus.placeholder', clearPlaceholder);
					jQueryinput
						.data({
							'placeholder-textinput': jQueryreplacement,
							'placeholder-id': id
						})
						.before(jQueryreplacement);
				}
				jQueryinput = jQueryinput.removeAttr('id').hide().prev().attr('id', id).show();
				// Note: `jQueryinput[0] != input` now!
			}
			jQueryinput.addClass('placeholder');
			jQueryinput[0].value = jQueryinput.attr('placeholder');
		} else {
			jQueryinput.removeClass('placeholder');
		}
	}

}(this, document, jQuery));

jQuery(function(){
jQuery(':input[placeholder]').placeholder();
});	
